class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {
        if(s1.equals("") && s2.equals("") && s3.equals("")) return true;
        int m = s1.length();
        int n = s2.length();
        int len = s3.length();
        if(m + n != len) return false;
        s1 = " " + s1;
        s2 = " " + s2;
        s3 = " " + s3;
        char[] chArr1 = s1.toCharArray();
        char[] chArr2 = s2.toCharArray();
        char[] chArr3 = s3.toCharArray();
        boolean[][] dp = new boolean[m + 1][n + 1];
        // 初始化
        for(int j = 1; j <= n; j++){
            if(chArr2[j] == chArr3[j]) dp[0][j] = true;
            else break;
        }
        for(int i = 1; i <= m; i++){
            if(chArr1[i] == chArr3[i]) dp[i][0] = true;
            else break;
        }
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(chArr1[i] == chArr3[i + j] && chArr2[j] == chArr3[i + j])
                    dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                else if(chArr1[i] == chArr3[i + j]) dp[i][j] = dp[i - 1][j];
                else if(chArr2[j] == chArr3[i + j]) dp[i][j] = dp[i][j - 1];
            }
        }
        return dp[m][n];
    }
}